package com.wing.order.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.config.BaseDataInit;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.enterprise.product.model.request.CabInventoryProductForm;
import com.wing.platform.feign.PlatformClient;
import com.wing.platform.order.model.entity.ProductDetail;
import com.wing.platform.order.model.response.InventoryJobBoxLabelVO;
import com.wing.platform.order.model.response.InventoryProductVO;
import com.wing.platform.order.model.response.ProductDetailVO;
import com.wing.warehouse.model.response.BoxLabelVO;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-云仓产品列表", tags = "#/order/cloudInventoryProduct")
@RestController("cloudInventoryProductController")
@RequestMapping("order/admin/cloudInventoryProduct")
@RequiredArgsConstructor
public class CloudInventoryProductController {

    private final EnterpriseClient enterpriseClient;
    private final PlatformClient platformClient;

    @ApiOperation(value = "查询所有云仓产品", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")

    })
    @GetMapping("/list")
    public PageResult<ProductDetail> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            ,@RequestParam(value = "skuName",required = false) String skuName
            ,@RequestParam(value = "skuCode",required = false) String skuCode
            ,@RequestParam(value = "productCategoryId",required = false) String productCategoryId
            ,@RequestParam(value = "companyName",required = false) String companyName

    ){
        //搜索
        String enterpriseId = String.valueOf(JwtUtils.getEnterpriseId());
        PageResult<ProductDetail> list = platformClient.getInventoryProductList(page,limit,sort,order,skuName,skuCode,productCategoryId,companyName,enterpriseId);
        return new PageResult<>(list.getData(), list.getCount());
    }

    @ApiOperation(value = "查询单个云仓产品", notes = "搜索")
    @GetMapping("/detailById")
    public JsonResult detailList(@RequestParam(value = "id",required = false)Long id){
        ProductDetailVO detail = platformClient.detailById(id);
        return JsonResult.success(detail);
    }

    @GetMapping("/addProductInventory")
    public JsonResult addProductInventory(@RequestParam(value = "productSkuCodeArr[]") String[] productSkuCodeArr){
        for (String item : productSkuCodeArr){
            CabInventoryProductForm cabInventoryProduct = new CabInventoryProductForm();
            cabInventoryProduct.setSkuCode(item);
            enterpriseClient.addCabInventoryProduct(cabInventoryProduct);
            enterpriseClient.updateProductDetail(item);
        }
        return JsonResult.success("操作成功");
    }

    @GetMapping("/getProductInventoryBySkuCode")
    public JsonResult<List<InventoryProductVO>> getProductInventoryBySkuCode(String skuCode)
    {
        return JsonResult.success(platformClient.getRepertoryBySkuCodeByCompanyId(skuCode, ""));
    }

    @ApiOperation(value = "根据skuCode和warehouseId查询料箱", notes = "查询")
    @GetMapping("/getBoxLabBySkuCodeAndWarehouseId")
    public JsonResult<List<InventoryJobBoxLabelVO>> getBoxLabBySkuCodeAndWarehouseId(@RequestParam("skuCode") String skuCode
            ,@RequestParam("warehouseId") Long warehouseId)
    {
        return JsonResult.success(platformClient.getBoxLabBySkuCodeAndWarehouseId(skuCode, warehouseId));
    }

    @ApiOperation(value = "根据skuCode和warehouseId统计料箱和库位", notes = "查询")
    @GetMapping("/getStorageWorkBinBySkuAndWarehouseId")
    public JsonResult<ProductDetailVO> getStorageWorkBinBySkuAndWarehouseId(
            @RequestParam("skuCode") String skuCode,@RequestParam("warehouseId") Long warehouseId){
        return JsonResult.success(platformClient.getStorageWorkBinBySkuAndWarehouseId(skuCode,warehouseId));
    }


}
